package com.feng.yupaoback.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> myRedisTemplate = new RedisTemplate<>();
        myRedisTemplate.setConnectionFactory(connectionFactory);

        // 自定义序列化
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();

        myRedisTemplate.setKeySerializer(stringSerializer);
        myRedisTemplate.setValueSerializer(jsonSerializer);
        myRedisTemplate.setHashKeySerializer(stringSerializer);
        myRedisTemplate.setHashValueSerializer(jsonSerializer);

        myRedisTemplate.afterPropertiesSet();
        return myRedisTemplate;
    }
}